Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic when restricted-std is enabled. #104971

Closed

Conversation

avafloww
Copy link

When restricted-std is in use, we do not have access to thread local storage, as sys/unsupported does not implement it. This previously would cause an infinite panic loop, as sys/unsupported/thread_local_key.rs would panic upon attempting to increment the local thread's panic count.

When `restricted-std` is in use, we do not have access to thread local storage,
as `sys/unsupported` does not implement it. This previously would cause an
infinite panic loop, as `sys/unsupported/thread_local_key.rs` would panic upon
attempting to increment the local thread's panic count.
@rustbot
Copy link
Collaborator

rustbot commented Nov 27, 2022

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Mark-Simulacrum (or someone else) soon.

Please see the contribution instructions for more information.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 27, 2022
@rustbot
Copy link
Collaborator

rustbot commented Nov 27, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

Comment on lines +341 to +346
// In a restricted_std environment, we likely don't have thread_local.
// If we attempt to use LOCAL_PANIC_COUNT here, it is probable that another panic will
// occur, sending us into an infinite panic loop that never calls the panic handler.
#[cfg(not(feature = "restricted-std"))]
if !must_abort {
panics = LOCAL_PANIC_COUNT.with(|c| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "likely" mean here? Your PR description says

When restricted-std is in use, we do not have access to thread local storage, as sys/unsupported does not implement it.

Is that always the case when using restricted-std? and if not, when can the behavior be different?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at it a bit more, it seems it's true most of the time, but not necessarily always. The restricted-std requirement is defined here if a platform match is not found, and since thread_local is unimplemented in sys/unsupported, a panic loop occurs.

There do appear to be some cases where this may not always be true - i.e. in the case of aarch64-apple-tvos, which is not allowed to use regular unrestricted std, despite being part of the unix family and thus using the unix system module which does properly implement thread-local support. I would imagine this same situation would also apply to any of the other targets which are called out in the same area in build.rs and also use a non-unsupported system module.

I'm not really sure how big of an impact this would be; since restricted-std is infectious, requiring itself to be in every std-dependent crate throughout the entire dependency tree in order to be used, it seems like a minor detail, especially since a double panic on these platforms will still trigger an abort via the global panic counter.

@jyn514
Copy link
Member

jyn514 commented Nov 28, 2022

I worry that this will just cause a panic loop in a different way, because double-panics no longer abort the process. Maybe we should force enable must_abort for restricted_std?

@bjorn3
Copy link
Member

bjorn3 commented Nov 29, 2022

Isn't restricted-std meant to be enabled for cases where libstd isn't allowed to be used and thus doesn't define the panic handler in the first place.

@avafloww
Copy link
Author

avafloww commented Dec 1, 2022

Sorry for taking a few days to respond - work has been busy! 😅

I worry that this will just cause a panic loop in a different way, because double-panics no longer abort the process. Maybe we should force enable must_abort for restricted_std?

@jyn514 The GLOBAL_PANIC_COUNT is still incremented and returned from increase, so even without use of LOCAL_PANIC_COUNT, double panics will still abort all the same.

I wonder if there's any way to effectively unit-test this? Seems a bit tricky, but I'm not too familiar with the repo...

Isn't restricted-std meant to be enabled for cases where libstd isn't allowed to be used and thus doesn't define the panic handler in the first place.

@bjorn3 The panic handler is still defined in a restricted-std environment, because #![feature(restricted_std)] being defined in all std-consuming crates is what allows std to be used in an otherwise no-std environment.

@bjorn3
Copy link
Member

bjorn3 commented Dec 1, 2022

restricted-std was introduced with -Zbuild-std in #74033. My impression of this PR is that restricted-std was to be meant to prevent using libstd entirely for targets that libstd doesn't yet support. As such you shouldn't use libstd at all in those cases. Not even to allow compilation of crates that use libstd. Standard library crates depending on libstd have to enable restricted-std to make sure they compile if -Zbuild-std automatically builds it for you even though they aren't allowed to be used either in those cases.

@Mark-Simulacrum
Copy link
Member

r? @bjorn3

@rustbot rustbot assigned bjorn3 and unassigned Mark-Simulacrum Dec 5, 2022
@anden3 anden3 added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 5, 2023
@anden3
Copy link
Contributor

anden3 commented Apr 5, 2023

Hello @avafloww! I pinged you as part of the triage procedure because this PR has received a review :)

@JohnCSimon
Copy link
Member

@avafloww

Ping from triage: I'm closing this due to inactivity, last time was november 2022
Please reopen when you are ready to continue with this.
Note: if you are going to continue please open the PR BEFORE you push to it, else you won't be able to reopen - this is a quirk of github.
Thanks for your contribution.

@rustbot label: +S-inactive

@JohnCSimon JohnCSimon closed this May 28, 2023
@rustbot rustbot added the S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. label May 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants